package com.doctor.comm;

import java.lang.reflect.Field;
import java.util.Calendar;
import java.util.List;
import java.util.Map;

import com.doctor.adapter.MultipleDialogAdapter;
import com.doctor.bean.ContactsBean;
import com.doctor.ui.R;

import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.app.DatePickerDialog.OnDateSetListener;
import android.app.TimePickerDialog.OnTimeSetListener;
import android.content.Context;
import android.content.DialogInterface;
import android.net.Uri;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.TimePicker;

public class DialogHelper {

	public interface DialogSelectCallback{
		public void onDialogSelected(int position);
	}
	
	/**
	 * 普通选择对话框
	 */
	public static void selectDialog(Context context,String titleStr,String[] operationStr,final DialogSelectCallback callback) {
		final Dialog dialog=new Dialog(context, R.style.myDialogTheme);
		dialog.setContentView(R.layout.operation_dialog);
		TextView title=(TextView) dialog.findViewById(R.id.dialogTitle);
		if (null!=titleStr && !"".equals(titleStr)) {
			title.setText(titleStr);
		}
		ListView listView=(ListView) dialog.findViewById(R.id.listView);
		
		ArrayAdapter<String> adapter=new ArrayAdapter<String>(context, R.layout.operation_dialog_item,
				R.id.operation_dialog_item, operationStr);
		listView.setAdapter(adapter);
		dialog.show();
		listView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// TODO Auto-generated method stub
				callback.onDialogSelected(arg2);
				dialog.dismiss();
			}
		});
	}
	
	/**
	 * 普通选择对话框
	 */
	public static void selectDialog(Context context,String titleStr,List<String> operationStr,final DialogSelectCallback callback) {
		final Dialog dialog=new Dialog(context, R.style.myDialogTheme);
		dialog.setContentView(R.layout.operation_dialog);
		TextView title=(TextView) dialog.findViewById(R.id.dialogTitle);
		if (null!=titleStr && !"".equals(titleStr)) {
			title.setText(titleStr);
		}
		ListView listView=(ListView) dialog.findViewById(R.id.listView);
		
		ArrayAdapter<String> adapter=new ArrayAdapter<String>(context, R.layout.operation_dialog_item,
				R.id.operation_dialog_item, operationStr);
		listView.setAdapter(adapter);
		dialog.show();
		listView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// TODO Auto-generated method stub
				callback.onDialogSelected(arg2);
				dialog.dismiss();
			}
		});
	}
	
	
	
	public interface NoticeDialogCallback{
		public void onClicked(int position);
	}
	
	/**
	 * 有取消和确定的对话框
	 */
	public static void noticeDialog(Context context,String noticeStr,final NoticeDialogCallback callback) {
		final Dialog dialog=new Dialog(context,R.style.myDialogTheme);
		dialog.setContentView(R.layout.notice_dialog);
		TextView notice=(TextView) dialog.findViewById(R.id.noticeTxtContent);
		if (null!=noticeStr && !"".equals(noticeStr)) {
			notice.setText(noticeStr);
		}
		
		Button cancelBtn=(Button) dialog.findViewById(R.id.cancelBtn);
		Button confirmBtn=(Button) dialog.findViewById(R.id.confirmBtn);
		cancelBtn.setOnClickListener(new OnClickListener() {			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				dialog.dismiss();
				callback.onClicked(0);
			}
		});
		confirmBtn.setOnClickListener(new OnClickListener() {			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				dialog.dismiss();
				callback.onClicked(1);
			}
		});
		dialog.setCancelable(false);
		dialog.show();
	}
	
	
	/**
	 * 只有一个确定按钮的对话框
	 * @param context
	 * @param noticeStr
	 */
	public static void showConfirmDialog(Context context, String noticeStr){
		AlertDialog.Builder builder = new AlertDialog.Builder(context, R.style.common_dialog);
		builder.setTitle("提示");
		builder.setIcon(R.drawable.alert_icon);
		if (noticeStr != null && noticeStr.length() > 0) {
			builder.setMessage(noticeStr);
		}	
		builder.setPositiveButton("确定", null);
		builder.setCancelable(false);
		builder.create().show();
	}
	
	
	/**
	 * 有取消和确定按钮的对话框
	 * @param context
	 * @param noticeStr
	 * @param positiveListener 确定按钮点击事件
	 */
	public static void showConfirmCancelDialog(Context context, String noticeStr, DialogInterface.OnClickListener positiveListener){
		AlertDialog.Builder builder = new AlertDialog.Builder(context, R.style.common_dialog);
		builder.setTitle("提示");
		builder.setIcon(R.drawable.alert_icon);
		if (noticeStr != null && noticeStr.length() > 0) {
			builder.setMessage(noticeStr);
		}	
		builder.setPositiveButton("确定", positiveListener);
		builder.setNegativeButton("取消", null);
		builder.setCancelable(false);
		builder.create().show();
	}
	
	
	
	public interface MultipleDialogCallback{
		public void onConfirmClicked(Map<Integer, ContactsBean> map);
	}
	
	/**
	 * 有选项和取消确定按钮的对话框
	 * @param context
	 * @param titleStr
	 * @param beanList
	 * @param callback
	 */
	public static void showMultipleDialog(Context context,String titleStr,List<ContactsBean> beanList,final MultipleDialogCallback callback){
		final Dialog dialog=new Dialog(context, R.style.myDialogTheme);
		dialog.setContentView(R.layout.multiple_dialog);
		TextView title=(TextView) dialog.findViewById(R.id.dialogTitle);
		if (null!=titleStr && !"".equals(titleStr)) {
			title.setText(titleStr);
		}
		
		ListView listView=(ListView) dialog.findViewById(R.id.listView);
		final MultipleDialogAdapter adapter=new MultipleDialogAdapter(context,beanList);
		listView.setAdapter(adapter);
		listView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// TODO Auto-generated method stub
				ContactsBean bean=(ContactsBean) arg1.getTag(R.id.bean_tag);
				adapter.changeChecked(bean);
			}
		});
		
		Button cancelBtn=(Button) dialog.findViewById(R.id.cancelBtn);
		cancelBtn.setOnClickListener(new OnClickListener() {			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				dialog.dismiss();
			}
		});
		Button confirmBtn=(Button) dialog.findViewById(R.id.confirmBtn);
		confirmBtn.setOnClickListener(new OnClickListener() {			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				dialog.dismiss();
				callback.onConfirmClicked(adapter.getAllSelected());
			}
		});
		dialog.setCancelable(false);
		dialog.show();
	}
	
	
	
	/**
	 * 弹出窗口显示图片
	 * @param context
	 * @param imageUri
	 */
	public static void showImageDialog(Context context, Uri imageUri){
		final Dialog dialog=new Dialog(context, R.style.myDialogTheme);
		dialog.setContentView(R.layout.image_dialog);
		ImageView img=(ImageView) dialog.findViewById(R.id.img);
		img.setImageURI(imageUri);
		
		ImageView closeBtn=(ImageView) dialog.findViewById(R.id.closeBtn);
		closeBtn.setOnClickListener(new OnClickListener() {			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				dialog.dismiss();
			}
		});
		
		dialog.setCancelable(false);
		dialog.show();
	}
	
	
	/**
	 * 通过反射使dialog点击按钮后不消失
	 * @param dialog
	 * @param dissmiss 是否消失
	 */
	public static void changeMshowing(Dialog dialog, boolean dissmiss) {
		if (dialog == null)
			return;
		
		try {
			Field field = dialog.getClass().getSuperclass().getDeclaredField("mShowing");
			field.setAccessible(true);
			field.set(dialog, dissmiss);
			dialog.dismiss();
		} catch (NoSuchFieldException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
	}
	
	
	/**
	 * 显示从控件上获取默认日期的日期设置窗口
	 * @param context
	 * @param tv
	 * @param calendar
	 * @param showZero 月日不足两位数时是否显示前面的0
	 */
	public static void showDatePickerDialog(Context context, final TextView tv, Calendar calendar, final boolean showZero) {
		//默认的年月日
		int defaultYear = calendar.get(Calendar.YEAR);
		int defaultMonth = calendar.get(Calendar.MONTH);
		int defaultDay = calendar.get(Calendar.DAY_OF_MONTH);
		CharSequence sequence = tv.getText();
		if (sequence != null) {
			String defaultDate = sequence.toString();
			if (defaultDate != null && defaultDate.length() > 0) {
				String[] dateArray = defaultDate.split("-");
				if (dateArray != null && dateArray.length == 3) {
					try {
						defaultYear = Integer.parseInt(dateArray[0]);
						defaultMonth = Integer.parseInt(dateArray[1]) - 1;
						defaultMonth = defaultMonth < 0 ? 0 :defaultMonth;
						defaultDay = Integer.parseInt(dateArray[2]);
					} catch (NumberFormatException e) {
						// TODO: handle exception
						e.printStackTrace();
						defaultYear = calendar.get(Calendar.YEAR);
						defaultMonth = calendar.get(Calendar.MONTH);
						defaultDay = calendar.get(Calendar.DAY_OF_MONTH);
					}
				}
			}
		}
		
		new DatePickerDialog(context, new OnDateSetListener() {					
			@Override
			public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
				// TODO Auto-generated method stub
				int month = monthOfYear + 1;
				String monthText = showZero && month < 10 ? "0" + month : String.valueOf(month);
				String dayText = showZero && dayOfMonth < 10 ? "0" + dayOfMonth : String.valueOf(dayOfMonth);
				tv.setText(year + "-" + monthText + "-" + dayText);
			}
		}, defaultYear, defaultMonth, defaultDay).show();
	}
	
	
	/**
	 * 显示从控件上获取默认时间的时间设置窗口
	 * @param context
	 * @param tv
	 * @param calendar
	 * @param showZero 时分不足两位数时是否显示前面的0
	 */
	public static void showTimePickerDialog(Context context, final TextView tv, Calendar calendar, final boolean showZero) {
		//默认的时分
		int defaultHour = calendar.get(Calendar.HOUR_OF_DAY);
		int defaultMinute = calendar.get(Calendar.MINUTE);
		CharSequence sequence = tv.getText();
		if (sequence != null) {
			String defaultTime = sequence.toString();
			if (defaultTime != null && defaultTime.length() > 0) {
				String[] timeArray = defaultTime.split(":");
				if (timeArray != null && timeArray.length == 2) {
					try {
						defaultHour = Integer.parseInt(timeArray[0]);
						defaultMinute = Integer.parseInt(timeArray[1]);
					} catch (NumberFormatException e) {
						// TODO: handle exception
						e.printStackTrace();
						defaultHour = calendar.get(Calendar.HOUR_OF_DAY);
						defaultMinute = calendar.get(Calendar.MINUTE);
					}
				}
			}
		}
		
		new TimePickerDialog(context, new OnTimeSetListener() {					
			@Override
			public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
				// TODO Auto-generated method stub
				tv.setText((showZero && hourOfDay < 10 ? "0" + hourOfDay : hourOfDay) + ":" + (showZero && minute < 10 ? "0" + minute : minute));
			}
		}, defaultHour, defaultMinute, true).show();
	}
	
}
